package com.ycxy.sh.transaction.controller.user;

import com.ycxy.sh.transaction.dao.dto.UserInfoStandardDTO;
import com.ycxy.sh.transaction.dao.entity.Login;
import com.ycxy.sh.transaction.dao.entity.UserInfo;
import com.ycxy.sh.transaction.dao.vo.UserInfoAvatarVo;
import com.ycxy.sh.transaction.dao.vo.UserInfoStandardVo;
import com.ycxy.sh.transaction.mapper.UserInfoMapper;
import com.ycxy.sh.transaction.service.LoginService;
import com.ycxy.sh.transaction.service.UserInfoService;
import com.ycxy.sh.transaction.util.GetCode;
import com.ycxy.sh.transaction.util.JsonReader;
import com.ycxy.sh.transaction.util.JustPhone;
import com.ycxy.sh.transaction.util.SmsUtil;
import com.ycxy.sh.transaction.web.JsonResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 个人中心 控制器
 *
 * @author hlt
 * @since 2019-12-21
 */
@Slf4j
@Api(tags = "11.用户个人主页管理模块")
@Controller
public class UserController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserInfoMapper userInfoMapper;


    /**
     * 修改密码
     * 1.前端传入旧密码（oldpwd）、新密码（newpwd）
     * 2.判断输入旧密码和系统旧密码是否相等
     * 4.修改密码
     */
    @ResponseBody
    @PutMapping("/user/updatepwd")
    public JsonResult updatePassword(HttpSession session, HttpServletRequest request) throws IOException {
        userInfoService.updatePassword(session,request);
        log.debug("修改密码成功");
        return JsonResult.ok();
    }

    /**
     * 展示用户头像昵称
     */
    @ResponseBody
    @PostMapping("/user/avatar")
    public JsonResult<UserInfoAvatarVo> selectUserAvatar(HttpSession session) {
        UserInfoAvatarVo userInfoAvatarVo = userInfoService.selectUserAvatar(session);
        log.debug("查询用户头像成功");
        return JsonResult.ok(userInfoAvatarVo);
    }

    /**
     * MultipartFile为Spring框架中实现文件上传、下载
     * 修改头像
     * */
    @PostMapping(value = "/user/updateuimg")
    @ResponseBody
    public JSONObject updateUserimage(@RequestParam(value = "file", required = false) MultipartFile file, HttpSession session) throws IOException {
        JSONObject jsonObject = userInfoService.updateUserimage(file, session);
        log.debug("修改头像成功");
        return jsonObject;
    }

    /**
     * 展示个人信息
     */
    @RequiresPermissions("user:userinfo")
    @GetMapping("/user/lookinfo")
    public String selectUserInfo(HttpSession session, ModelMap modelMap) {
        UserInfoStandardVo userInfoStandardVo = userInfoService.selectUserInfo(session);
        modelMap.put("userInfo",userInfoStandardVo);
        log.debug("成功展示用户的个人信息");
        return "/user/userinfo";
    }

    /**
     * 跳转到完善个人信息
     */
    @GetMapping("/user/perfectinfo")
    public String perfectInfo(HttpSession session, ModelMap modelMap) {
        Long userid = (Long) session.getAttribute("userid");
        UserInfoStandardVo userInfoStandardVo = userInfoMapper.selectUserinfo(userid);
        modelMap.put("perfectInfo",userInfoStandardVo);
        return "/user/perfectInfo";
    }

    /**
     * 修改个人信息
     * 1.前端传入用户昵称（username）、用户邮箱（email）、性别（sex）、学校（school）、院系（faculty）、入学时间（startime）
     * 2.前端传入变更后的字段，未变更的不传入后台
     * 3.判断更改的用户名是否已存在
     * 4.修改个人信息
     */
    @ResponseBody
    @PostMapping("/user/updateinfo")
    public JsonResult updateInfo(@RequestBody UserInfoStandardDTO userInfoStandardDTO, HttpSession session) {
        userInfoService.updateInfo(userInfoStandardDTO,session);
        log.debug("用户个人信息修改成功");
        return JsonResult.ok();
    }

    /**更换手机号时发送短信验证码
     * 1.判断是否为更换手机号类型验证码
     * 2.判断手机号格式是否正确
     * 3.查询账号是否存在
     * 4.发送验证码
     * */
    @ResponseBody
    @PostMapping("/user/sendupdatephone")
    public JsonResult sendUpdatePhone(HttpServletRequest request) throws IOException {
       userInfoService.sendUpdatePhone(request);
        log.debug("发送短信验证码成功");
        return JsonResult.ok();
    }

    /**
     * 修改绑定手机号
     * 1.获取session中userid
     * 2.修改login和userInfo中对应的手机号
     */
    @ResponseBody
    @PostMapping("/user/updatephone/{mobilephone}/{vercode}")
    public JsonResult updatePhone(@PathVariable("mobilephone")String mobilephone, @PathVariable("vercode")String vercode, HttpSession session) {
        userInfoService.updatePhone(mobilephone,vercode,session);
        log.debug("发送短信验证码成功");
        return JsonResult.ok();
    }
}

